package com.sdweizan.sdk.bytedance.util;

import com.alibaba.fastjson.TypeReference;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpRequestBase;
import org.apache.http.client.utils.URIBuilder;

import java.net.URI;
import java.util.Map;

@Slf4j
public class HttpClientGetRequestExecutor<T> extends HttpClientRequestExecutor<T> {

  public HttpClientGetRequestExecutor(TypeReference<T> typeReference) {
    super(typeReference);
  }

  @Override
  protected HttpRequestBase buildRequest(String requestUri, Map<String, Object> requestBody) {
    URIBuilder uriBuilder = new URIBuilder(URI.create(requestUri));
    if (requestBody != null) {
      requestBody.keySet().stream()
          .filter(key -> requestBody.get(key) != null)
          .forEach(key -> uriBuilder.addParameter(key, requestBody.get(key).toString()));
    }
    String requestUrl = uriBuilder.toString();
    log.info("访问字节服务接口：{}", requestUrl);
    return new HttpGet(requestUrl);
  }
}
